#pragma once
#include <QMouseEvent>

class Node;
class HierarchyTreeModel;
class MoveNodeCommand;

//Manage All the Nodes.
//Builds a HierarchyTreeModel internal, which could be use for tree displaying.
class Scene
{
public:
	Scene(void);
	Scene(Node* root);
	virtual ~Scene(void);

	static Node* createEmpty();
	static Node* createSprite();
	static Node* createLayer();

	static void freeNode(Node* node);
private:
	Node* root;
	HierarchyTreeModel* model;
	MoveNodeCommand* moveCommand;
public:
	Node* getRoot() { return root; }
	HierarchyTreeModel* treeModel() { return model; }

	void addNode(Node* node);
	void removeNode(Node* node);
	void setCurrentNode(Node* node);
	void updateNode(Node* node);
	void clearSelection();

	//Manipulating

	//Manipulating Node
	//invokes MoveNodeCommand
	void beginMove(QMouseEvent* event);
	void doMove(QMouseEvent* event);
	void endMove(QMouseEvent* event);

	//Manipulating View
	void beginMoveView(QMouseEvent* event);
	void doMoveView(QMouseEvent* event);
	void endMoveView(QMouseEvent* event);

	void beginScaleView(QMouseEvent* event);
	void doScaleView(QMouseEvent* event);
	void endScaleView(QMouseEvent* event);

	bool isHandlingAction() const;

	Node* hitNode(QPointF& point, Node* node = 0);
	void notifyNodeChange(Node* node);

	//
	void keyPressEvent(QKeyEvent* event);
	void keyReleaseEvent(QKeyEvent* event);

	void paint(QPainter& painter);
	void updateView();
private:
	void paintNode(QPainter& painter, Node* node);
	void clearNodeSelection(Node* node);
	void findSelection(Node* node, QList<Node*>& out);

	//state
	Node* m_pOperatingNode;
	bool isMovingView;
	bool isScalingView;
	float m_OperatingOffX, m_OperatingOffY;
};
